﻿package classes 
{
	import alternativa.engine3d.core.Mesh;
	import alternativa.engine3d.core.Surface;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.loaders.Loader3DS;
	import alternativa.engine3d.events.MouseEvent3D;
	import alternativa.engine3d.materials.TextureMaterialPrecision;
	import alternativa.types.Texture;
	import alternativa.utils.MeshUtils;
	import alternativa.utils.MathUtils;
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.engine3d.materials.SurfaceMaterial;
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.engine3d.materials.Material;
	import alternativa.engine3d.materials.MovieClipMaterial;
	import alternativa.engine3d.primitives.Plane;
	import alternativa.engine3d.core.Face;
	
	import flash.events.Event;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.utils.getTimer;
	import flash.geom.Matrix;
	import flash.display.BlendMode;
	
	import classes.loadExternalSWF;

	public class showModel3D extends Mesh
	{
		private var swfDmm:loadExternalSWF;
		private var swfDmmMaterial:MovieClipMaterial;
		
		public var model3d:Object3D;
		private var clonePantalla:Object3D;
		private var loader:Loader3DS;
		private var factorScaleModel:uint;
		private var cloneWall:Object3D;
		private var n:Number = 0.5;
		
		private var temporizadorElevador:Number = 0;
		private var topeEvelador:Number = 20;
		private var initialPositioElevator:Number;
			
		public function showModel3D(pathToModel:String = "../model/prueba001.3ds", factorScale:uint = 1):void 
		{	
			factorScaleModel = factorScale;
			
			model3d = new Object3D();
			
			//swfDmm = new loadExternalSWF("swf/desarrollomultimedia.swf"); //920x325
			swfDmm = new loadExternalSWF("swf/publicity.swf"); //300x200
			//swfDmm = new loadExternalSWF("swf/test.swf"); //300x200
			swfDmmMaterial = new MovieClipMaterial(swfDmm, 300, 300);
			
			loader = new Loader3DS();
			loader.smooth = true;
			//loader.precision = TextureMaterialPrecision.HIGH;
			loader.addEventListener(Event.ACTIVATE, onInitLoading);
			loader.addEventListener(Event.COMPLETE, onLoadingComplete);
			loader.load(pathToModel);
		}
		
		private function onInitLoading(e:Event):void 
		{
			trace("****cargando"+e.target);
		}
		
		private function onLoadingComplete(e:Event):void 
		{
			loader.removeEventListener(Event.ACTIVATE, onInitLoading);
			loader.removeEventListener(Event.COMPLETE, onLoadingComplete);
			clonePantalla = new Object3D();
			
			for (var o:* in loader.content.children) 
			{
				var object:Object3D = new Object3D();
				object = o;
				trace("Model3D->Mesh->content.children--> " + object.name + " <--");
				
				if (object.name == "paredMovil") 
				{
						
						cloneWall = object;
						cloneWall.addEventListener(MouseEvent3D.CLICK, listenMove);
						
						model3d.addChild(cloneWall);
				}
				else 
				{
					model3d.addChild(object);
				}
				
				if (object.name == "pantalla") 
				{
					clonePantalla = object;
					clonePantalla.addEventListener(MouseEvent3D.CLICK, pantallaOnClick);	
					
					Mesh(clonePantalla).cloneMaterialToAllSurfaces(swfDmmMaterial);
					model3d.addChild(clonePantalla);
					
				} 
				else 
				{
					model3d.addChild(object);
				}
				
				weldVerticesAndFaces(object);	
			}
			model3d.scaleX = model3d.scaleY = model3d.scaleZ = factorScaleModel;
			
			if (model3d.getChildByName("elevador"))
			{
				initialPositioElevator = model3d.getChildByName("elevador").z;
			}
			addChild(model3d)
			checkFloorColision();
		}
		
		private function listenMove(e:MouseEvent3D):void
		{
			var timer:Timer = new Timer(10, 100);
			timer.addEventListener(TimerEvent.TIMER, moveWall);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, changeDirection);
			timer.start();
			//cloneWall.x += 1;
		}
		
		private function moveWall(e:TimerEvent):void 
		{
			model3d.getChildByName("paredMovil").x += n;
		}
		private function changeDirection(e:TimerEvent):void 
		{
			n = n * ( -1) ;
		}
		
		private function pantallaOnClick(e:MouseEvent3D):void 
		{
			trace(e.target);
			clonePantalla.z -= 4;
		}
		
		private function weldVerticesAndFaces(object:Object3D):void 
		{
			if (object is Mesh) 
			{
				MeshUtils.autoWeldVertices(Mesh(object), 0.01);
				MeshUtils.autoWeldFaces(Mesh(object), 0.01, 0.001);
			}
			for (var key:* in object.children) 
			{
				weldVerticesAndFaces(key);
			}
		}
		
		private function checkFloorColision():void 
		{
            var timer:Timer = new Timer(1, 1);
			timer.addEventListener(TimerEvent.TIMER, rotar2);
			timer.start();
		}
		
		private function rotar1(e:TimerEvent):void 
		{				
			var timer2:Timer = new Timer(10, 900);
			timer2.addEventListener(TimerEvent.TIMER, rotarIda);
			timer2.addEventListener(TimerEvent.TIMER_COMPLETE, rotar2);
			timer2.start();									
		}
			
		private function rotarVuelta(e:TimerEvent):void 
		{								
			model3d.getChildByName("elevador").z -= 0.1;					
		}
		
		private function rotar2(e:TimerEvent):void 
		{
			var myTimer:Timer = new Timer(10, 900);
			myTimer.addEventListener(TimerEvent.TIMER, rotarVuelta);
			myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, wait);
			myTimer.start();								
		}
		
		private function rotarIda(e:TimerEvent):void 
		{
			model3d.getChildByName("elevador").z += 0.1;
		}
		
		private function wait(e:TimerEvent):void 
		{
			var myTimer:Timer = new Timer(10, 200);
			myTimer.addEventListener(TimerEvent.TIMER, esperar);
			myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, rotar1);
			myTimer.start();
		}
		
		private function esperar(e:TimerEvent):void 
		{
			//
		}
		
		/*private function moveElevator(e:TimerEvent):void 
		{
			if (model3d.getChildByName("elevador")) 
			{
				temporizadorElevador ++;
				model3d.getChildByName("elevador").z = initialPositioElevator + temporizadorElevador;
				if (model3d.getChildByName("elevador").z > topeEvelador) 
				{
					temporizadorElevador = -200;
				}
			} 
		}*/
	}
}